var app = getApp();
var maxPageIndex;
Page({
  data: {
    ifShownoData: true,
    pageIndex: 1,
    orderStatus: [
      {
        statusName: '待定价',
        order_type: '2'
      },
      {
        statusName: '待回皮',
        order_type: '7'
      }
    ],
    order_type: '2',
    result: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function(options) {
    wx.pageScrollTo({ scrollTop: 0, duration: 0 });
    this.setData({
      pageIndex: 1
    });
    this.getData();
  },
  //订单选择
  orderStatusList: function(e) {
    var that = this;
    that.setData({
      //把选中值放入判断值
      order_type: e.currentTarget.dataset.select
    });
    wx.pageScrollTo({ scrollTop: 0, duration: 0 });
    this.setData({
      pageIndex: 1
    });
    this.getData();
  },
  getData: function() {
    var that = this;
    wx.showLoading({ title: '加载中' });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'findOrderUser',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId,
        page: this.data.pageIndex, //当前页
        size: 8, //条数
        orderStatus: that.data.order_type,
        timeSection: 1
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res);
        if (res.data.code == 100) {
          let data = res.data.result;
          console.log(data);
          maxPageIndex = data.total / 8;

          if (that.data.pageIndex != 1) {
            var array = that.data.result;
            array = array.concat(data.result);
            that.setData({ result: array });
          } else {
            that.setData({ result: data.result });
          }
          that.setData({ ifShownoData: true });
        } else if (res.data.code == 400) {
          that.setData({ ifShownoData: false, result: [] });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          });
        }
      },
      fail: function(error) {}
    });
  },
  //
  toPrice(e) {
    let data = e.currentTarget.dataset.order;
    wx.navigateTo({ url: 'priced?data=' + JSON.stringify(data) });
  },
  //取消订单事件
  toCancel: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      // title: '提示',
      content: '确定取消该订单吗？',
      cancelText: '再等等',
      confirmColor: '#50BEC5',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            method: 'POST',
            url: app.preUrl + 'updateOrderStatus',
            dataType: 'json',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              id: id,
              orderStatus: 6
            },
            success: function(res) {
              if (res.data.code === 100) {
                that.setData({
                  pageIndex: 1
                });
                that.getData();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.message,
                  confirmColor: '#56BDE0',
                  showCancel: false
                });
              }
            },
            fail: function(error) {}
          });
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    });
  },
  onReachBottom: function() {
    var that = this;
    if (maxPageIndex <= that.data.pageIndex) {
      console.log('到底了');
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1
    });

    // 上拉获取更多数据
    that.getData();
  },
  onPullDownRefresh: function() {
    var that = this;
    that.setData({ pageIndex: 1, result: [] });
    this.getData();
    wx.stopPullDownRefresh();
  }
});
